package com.hmall.gateway.filter;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

@Component
@Slf4j
public class PrintAnyGatewayFilterFactory extends AbstractGatewayFilterFactory<PrintAnyGatewayFilterFactory.Config> {
    @Override
    public GatewayFilter apply(Config config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                log.info("路由拦截器放行。。。"+config);
                //ServerHttpResponse response = exchange.getResponse();
                //response.setRawStatusCode(401);
               // return response.setComplete();
                return chain.filter(exchange);
            }
        };
    }

    //定义一个类（泛型）
    @Data
    static class Config{
        private String name;
        private String city;
        private String sex;
    }

    //返回对象（泛型）的属性名字
    @Override
    public List<String> shortcutFieldOrder() {
        return List.of("name","city","sex");
    }

    //获取类（泛型）的字节码
    @Override
    public Class<Config> getConfigClass() {
        return Config.class;
    }
}
